


CLoserMe:
	SendInput, {Blind}!{F4}
return


TabCloserMe:
	SendInput, {Blind}^{F4}
return



;~ ~~~~~~~~~~~~~
;~ ;SUPER SHORTCUT

SUPER_SHORTS:
	
	COMMAND := USER_INI.UserHotkeys[A_ThisHotkey]
	execute(COMMAND)

return




MAIN_TRIGGER:	;{
	
	If (WinExist("c0bra Main GUI"))
	{
		if WinActive("c0bra Main GUI")
			SLR_GUI()
		else
		{
			Gui, Destroy
			goto, Gui
		}
	}
	else
	{
		IfWinExist SLR
		{
			IfWinActive SLR
				goto Gui
			else
				WinActivate SLR
		}
		else
			goto Gui
	}
	
	;}
return




RELOADME:
Reload
return





Gui:
Gui, 1:Destroy
Gui, 2:Destroy 

;SET VARIABLES

	VARSET()
	
	MouseGetPos, XPOS, YPOS ; get mouse position to show gui
	GUIoLDpOS:
	Gui, Color, % USER_INI.Colors.GuiBackColor ; gui background color
	

;~~~~~~~~
;GUI CALL

	gui, -caption +ToolWindow +AlwaysOnTop
	gui, margin, %GUI_SPACING%, %GUI_SPACING%
	
	MainHwnd := WinExist("A")
	
	; --- Search Bar ---
	if (SEARCH)
	{
		Gui, font, s%SEARCH_TEXT_SIZE% w400,  %BUTTON_FONT% 
		Gui, Add, Edit, x%GUI_SPACING% y%GUI_SPACING% w%SEARCH_WIDTH% h%SEARCH_HEIGHT% 0x200 vGSEARCH,
		Gui, Add, Text, xp yp-1 w%SEARCH_TEXT_WIDTH% h%SEARCH_HEIGHT%-2 0x200 Center +backgroundtrans hwndBackSearch, %SEARCH_BACK_TEXT%
		
		gui, font, s%TEXT_SIZE% w%TEXT_BOLD%, %BUTTON_FONT% ; gui button font size
		Gui, Add, text, x%GO_X% y%GUI_SPACING% w%GO_WIDTH% h%SEARCH_HEIGHT% 0x200 Center gButtonPress hwndGoHwnd, GO
			CTLCOLORS.Attach(GoHwnd, USER_INI.Colors.GOButtonColor ? USER_INI.Colors.GOButtonColor : USER_INI.Colors.ButtonColor
								, USER_INI.Colors.GOTextColor ? USER_INI.Colors.GOTextColor : USER_INI.Colors.TextColor)
	}
		
	; --- Buttons ---
	Gui, Font, s%TEXT_SIZE% w%TEXT_BOLD%, %BUTTON_FONT%

	loop, Parse, MAIN_BUTTONS, `n ; get count of button labels for gui adds below
		KEY_COUNT := A_Index

	loop, Parse, MAIN_BUTTONS, `n ; create gui based on labels
	{
		A_HWND := "back" A_Index
		bColor := USER_INI.Colors[nospace(A_LoopField) "ButtonColor"] ? USER_INI.Colors[nospace(A_LoopField) "ButtonColor"] : USER_INI.Colors.ButtonColor
		tColor := USER_INI.Colors[nospace(A_LoopField) "TextColor"] ? USER_INI.Colors[nospace(A_LoopField) "TextColor"] : USER_INI.Colors.TextColor
		
		if (A_Index = 1) ; first key
		{
			Gui, Add, text, x%GUI_SPACING% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
				CTLCOLORS.Attach(%A_HWND%, bCOlor, tColor)
			continue
		}
		
		else if (mod(A_Index, 2)) ; odd key
		{
			if (A_Index = KEY_COUNT) ; odd last key
			{
				LAST_X := GUI_SPACING + (BUTTON_WIDTH / 2) ; get x position for last button
				Gui, Add, text, x%LAST_X% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
					CTLCOLORS.Attach(%A_HWND%, bColor, tColor)
				continue
			}
			
			Gui, Add, text, x%GUI_SPACING% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
				CTLCOLORS.Attach(%A_HWND%, bColor, tColor)
			continue
		}
		
		else ; even key
		{
			Gui, Add, text, x+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
				CTLCOLORS.Attach(%A_HWND%, bColor, tColor)
			continue
		}
	}
	
	;--- Footer ---
	if (FOOTER_SHOW)
	{
		Gui, Font, s7 cWhite, %BUTTON_FONT% 
		Gui, Add, Text, x%GUI_SPACING% y+5 w%FOOTER_WIDTH% h10 0x200 Center hwndF, c0bra v%VERSION%
	}
		
	gui, show, x%XPOS% y%YPOS%, c0bra Main GUI

return


nospace(ByRef s)
{
	StringReplace, v, s, %A_Space%,, All
	return s
}


/*!
	Function: VARSET(configPath)
		Get GUI settings from config file.
		
	Parameters:
		configPath - Path to the config settings file.
*/
VARSET(configPath = "")
{
	global 
	;~ global var
	configPath := configPath ? configPath : COBRA "\user\user_settings.ini"
	
	USER_INI := ""
	USER_INI := new ini(configPath)
	var := "", var := {}
	
	PREV_HWND         := ""
	
		
	;~ var.GUI_SPACING := Trim(USER_INI.GuiInfo.Spacing)

	;~ ; --- GO Button ---
	;~ var.GO_WIDTH          := Trim(USER_INI.GuiInfo.GoWidth)
	;~ var.GO_X              := (BUTTON_WIDTH *2) + (GUI_SPACING * 2) - GO_WIDTH
	
	;~ ; --- Buttons ---
	;~ var.BUTTON_WIDTH      := Trim(USER_INI.GuiInfo.Width)
	;~ var.BUTTON_HEIGHT     := Trim(USER_INI.GuiInfo.Height)
	;~ var.BUTTON_FONT		  := Trim(USER_INI.GuiInfo.ButtonFont)
	
	;~ ; --- Search Bar ---
	;~ var.SEARCH            := Trim(USER_INI.GuiInfo.Search)
	;~ var.SEARCH_TEXT_SIZE  := Trim(USER_INI.GuiInfo.SearchText)
	;~ var.SEARCH_BACK_TEXT  := Trim(USER_INI.GuiInfo.BackText)
	;~ var.SEARCH_HEIGHT	  := Trim(USER_INI.GuiInfo.SearchHeight)
	;~ var.SEARCH_WIDTH      := (BUTTON_WIDTH * 2) - GO_WIDTH
	;~ var.SEARCH_TEXT_WIDTH := SEARCH_WIDTH - 8

	;~ ; --- Footer ---
	;~ var.FOOTER_WIDTH      := (BUTTON_WIDTH * 2) + GUI_SPACING
	;~ var.FOOTER_SHOW       := Trim(USER_INI.GuiInfo.Footer)

	;~ var.MAIN_BUTTONS	  := Trim(USER_INI.Keys("MainButtons"))

	;~ var.TEXT_SIZE         := Trim(USER_INI.GuiInfo.Text)
	;~ var.TEXT_BOLD         := Trim(USER_INI.GuiInfo.TextW)
	
	;~ return var
;~ }
	
	GUI_SPACING       := Trim(USER_INI.GuiInfo.Spacing)

	; --- Buttons ---
	BUTTON_WIDTH      := Trim(USER_INI.GuiInfo.Width)
	BUTTON_HEIGHT     := Trim(USER_INI.GuiInfo.Height)
	BUTTON_FONT		  := Trim(USER_INI.GuiInfo.ButtonFont)
	
	; --- GO Button ---
	GO_WIDTH          := Trim(USER_INI.GuiInfo.GoWidth)
	GO_X              := (BUTTON_WIDTH *2) + (GUI_SPACING * 2) - GO_WIDTH
	
	; --- Search Bar ---
	SEARCH            := Trim(USER_INI.GuiInfo.Search)
	SEARCH_TEXT_SIZE  := Trim(USER_INI.GuiInfo.SearchText)
	SEARCH_BACK_TEXT  := Trim(USER_INI.GuiInfo.BackText)
	SEARCH_HEIGHT	  := Trim(USER_INI.GuiInfo.SearchHeight)
	SEARCH_WIDTH      := (BUTTON_WIDTH * 2) - GO_WIDTH
	SEARCH_TEXT_WIDTH := SEARCH_WIDTH - 8

	; --- Footer ---
	FOOTER_WIDTH      := (BUTTON_WIDTH * 2) + GUI_SPACING
	FOOTER_SHOW       := Trim(USER_INI.GuiInfo.Footer)

	MAIN_BUTTONS	  := Trim(USER_INI.Keys("MainButtons"))

	TEXT_SIZE         := Trim(USER_INI.GuiInfo.Text)
	TEXT_BOLD         := Trim(USER_INI.GuiInfo.TextW)
}



;~~~~~~~~~~
;GUI 2 CALL

SIDE_GUI(NEWGUI)
{
	global
	
	CURR_GUI := A_gui
	NEXT_GUI := CURR_GUI = 1 ? CURR_GUI + 2 : CURR_GUI + 1
	
	SetTitleMatchMode, 3
	IfWinExist, %NEWGUI%
	{
		gui, %NEXT_GUI%:Destroy
		return
	}
	SetTitleMatchMode, 1
	
	PREV_GUI := CURR_GUI
	CURR_GUI := NEXT_GUI
	
	
	VARSET()
	
	
	Gui, Submit, nohide
	Gui, +LastFound
	WinGetPos,x,y,w,h

	GuiControlGet, CONTROL_POS, Pos, %A_GuiControl%
	
	if (PREV_GUI = 1)
		ON_LEFT := ON_LEFT(CONTROL_POSX)q
	
	if ON_LEFT
	{
		XPOS := x - (2 * GUI_SPACING) - BUTTON_WIDTH
		YPOS := y + CONTROL_POSY - GUI_SPACING
	}
	else
	{
		XPOS := x + CONTROL_POSX + BUTTON_WIDTH + GUI_SPACING
		YPOS := y + CONTROL_POSY - GUI_SPACING
	}
	
	Gui, %CURR_GUI%:Color, % USER_INI.Colors.Gui2BackColor ; gui 2 background color	
	
	Gui, %CURR_GUI%:-caption +ToolWindow +Owner%PREV_GUI%
	gui, %CURR_GUI%:margin, %GUI_SPACING%, %GUI_SPACING%
	gui, %CURR_GUI%:Font, s%TEXT_SIZE% w%TEXT_BOLD%, %BUTTON_FONT% 
	
	;Bookmarks
	if (NEWGUI = "Bookmarks")
	{
		loop, %A_MyDocuments%\..\Favorites\Links\*.url
		{
			if (A_LoopFileExt = "URL")
			{
				THE_BOOKMARK := RegExReplace(A_LoopFileName, "i)\.[^.]*$")
				THE_BOOKMARK_PATH := A_LoopFileLongPath
				
				A_HWND := "book" A_Index
				Gui, %CURR_GUI%:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center g3ButtonPress hwnd%A_HWND%, %THE_BOOKMARK%
					CTLCOLORS.Attach(%A_HWND%, USER_INI.Colors.Gui2ButtonColor, USER_INI.Colors.Gui2TextColor)
			}
		}
	}
	else
	{
		NEWGUI_KEYS := USER_INI.Keys(NEWGUI)
		Loop, Parse, NEWGUI_KEYS, `n
		{
			A_HWND := "side" A_Index
			bColor := USER_INI.Colors[nospace(A_LoopField) "ButtonColor"] ? USER_INI.Colors[nospace(A_LoopField) "ButtonColor"] : USER_INI.Colors.Gui2ButtonColor
			tColor := USER_INI.Colors[nospace(A_LoopField) "TextColor"] ? USER_INI.Colors[nospace(A_LoopField) "TextColor"] : USER_INI.Colors.Gui2TextColor
			
			Gui, %CURR_GUI%:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center g3ButtonPress hwnd%A_HWND%, % A_LoopField
				CTLCOLORS.Attach(%A_HWND%, bColor, tColor)
		}
	}
	
	Gui, %CURR_GUI%:Show, x%XPOS% y%YPOS%, %NEWGUI%
	return 
}


SLR_GUI()
{
global

Gui, 1:Destroy

;~~~~~~~~~~~~~
;SET VARIABLES

	VARSET()

MouseGetPos, MouseX1, MouseY1

gui, 2:font, s%TEXT_SIZE% w%TEXT_BOLD%, %BUTTON_FONT% 
Gui, 2:+toolwindow -caption
Gui, 2:Color, FF0000
Gui, 2:Margin, %GUI_SPACING%, %GUI_SPACING%

Gui, 2:Add, text, x%GUI_SPACING% y%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwndLOGOFF 0x200 Center gSLR, Logoff
	CTLCOLORS.Attach(LOGOFF, USER_INI.Colors.ButtonColor, USER_INI.Colors.TextColor)
Gui, 2:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwndRESTART 0x200 Center gSLR, Restart
	CTLCOLORS.Attach(RESTART, USER_INI.Colors.ButtonColor, USER_INI.Colors.TextColor)
Gui, 2:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwndSHUTDOWN 0x200 Center gSLR, Shutdown
	CTLCOLORS.Attach(SHUTDOWN, USER_INI.Colors.ButtonColor, USER_INI.Colors.TextColor)
Gui, 2:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwndCANCEL 0x200 Center gCancel, Cancel
	CTLCOLORS.Attach(CANCEL, USER_INI.Colors.ButtonColor, USER_INI.Colors.TextColor)

Gui, 2:Show, x%MouseX1% y%MouseY1%, SLR
}


SLR:
MsgBox,4,, SAVE ALL YOUR WORK BEFORE HITTING YES!!`n`nAre you sure you want to %A_GuiControl%?
IfMsgBox yes
{
	Gui, 1:Destroy
	Shutdown, % A_GuiControl = "Shutdown" ? "13" : A_GuiControl = "Restart" ? "6" : "0"
}
Else
	return
return




;___ Re-Size GUI Event Handler ________________________________________________________________________

GuiSize:
2GuiSize:
3GuiSize:
4GuiSize:
5GuiSize:
If (A_EventInfo != 1)
{
  Gui, %A_Gui%:+LastFound
  WinSet, ReDraw
}
return




;___ Close GUI Event Handler _____________________________________________________________________________


GuiClose:
2GuiClose:
3GuiClose:
4GuiClose:
5GuiClose:
GuiEscape:
2GuiEscape:
3GuiEscape:
4GuiEscape:
5GuiEscape:	
	CTLCOLORS.free()
	Gui, %A_Gui%:Destroy
return



;___ RIGHT-CLICK MENU ________________________________________________________________________________


GuiContextMenu:
2GuiContextMenu:
3GuiContextMenu:
4GuiContextMenu:
5GuiContextMenu:
	MouseGetPos, XPOS, YPOS
	btnTxt := A_GuiControl	
	meDisp := RegExReplace(btnTxt, "i)(.+)s$", "$1(s)")
	;		
	; Common menu header items
	;
	Menu, Title, Add
	Menu, Title, DeleteAll		
	menu, title, add, Cancel, QuickEditMenu
	menu, title, default, cancel
	menu, title, add,
	;
	; Add/Remove search bar
	;
	if (!USER_INI.GuiInfo.Search)
	{
		menu, title, add, Enable/Add Search Bar, ToggleSearchBar
		menu, title, add
	}
	else if (btnTxt = "GoHwnd")
	{
		MENU, title, add, Disable/Remove Search Bar, ToggleSearchBar
		menu, title, add
	}
	;
	; Determine if menu item has children
	;
	IniRead, myChildren, % USER_SETTINGS, %btnTxt%		
	If (myChildren)
	{   ;
		; Has children -->
		;
		Menu, Title, Add, Add a %btnTxt%, QuickEditMenu
		Menu, Title, Add, Edit %meDisp%, QuickEditMenu
	}
	else
	{	;
		; No children -->
		;
		Menu, Title, Add, Add a %btnTxt%, QuickEditMenu
		Menu, Title, Add, Delete %btnTxt% button, QuickEditMenu
		Menu, Title, Add, Edit %meDisp%, QuickEditMenu
	}
	;
	; Show the menu
	;
	Menu, Title, Color, % GUI_INI.ContextMenu.BackColor
	Menu, Title, Show, x%XPOS% y%YPOS%
return


EditButton()
{
}

; ***********************************
; Handler for context menu
; ***********************************
QuickEditMenu:
	if (Instr(A_ThisMenuItem, "Cancel"))
		return
	if (InStr(A_ThisMenuItem, "Add"))
	{	;
		; ADD
		;
		InputBox, n, Add Item, Button name:
		if (!ErrorLevel && n != "")
		{
			InputBox, p, Add Item, Button path/command:						
			IniWrite, %p%, %USER_SETTINGS%, %btnTxt%, %n%
			gui, Destroy			
		}
	}
	else if (InStr(A_ThisMenuItem, "Delete"))
	{	;
		; DELETE
		;
		MsgBox, 262196, Are you sure?, Delete this button? 
		ifmsgbox, Yes
		{
			RegExMatch(COMMAND, "i)\s*?\[\w+?\]\s*?(?P<Parent>\w+)\s*?", my)
			IniDelete, %USER_SETTINGS%, %myParent%, %btnTxt%
			Gui, Destroy
		}
	}
	else if (InStr(A_ThisMenuItem, "Edit"))
	{	;
		; EDIT
		;
		; TODO: Allow changing of button text & button's command
	}
	else
	{
		msgbox, 4144,, Sorry!`n`nThis feature is still under construction...
	}
return



ToggleSearchBar:
	USER_INI.GuiInfo.Search := ! USER_INI.GuiInfo.Search
	User_INI.Save(USER_SETTINGS)
	Gui, Destroy
	goto,guiOldPos
return



;{___  Context Menu: Edit GUI  _________________________________________________________________________

IniGui:
	
	Gui, ini:Add, Edit, x12 y30 w420 h30 , 
	Gui, ini:Add, Edit, x12 y90 w420 h30 , 
	Gui, ini:Font, S11 CDefault Bold,  %BUTTON_FONT% 
	Gui, ini:Add, Text, x12 y10 w100 h20 +BackgroundTrans, Label
	Gui, ini:Add, Text, x12 y70 w200 h20 +BackgroundTrans, Action/Command
	Gui, ini:Add, Button, x127 y140 w100 h40 , Cancel
	Gui, ini:Add, Button, x237 y140 w100 h40 , Submit

	Gui, ini:Show, w457 h195, Untitled GUI
return

iniGuiEscape:
iniGuiClose:
	Gui, ini:Destroy
return


ButtonSubmit:
	
return


ButtonCancel:
	
return

;}


ButtonPress:

	if !(WinExist(A_GuiControl))
		Gui, 3:Destroy
	GuiControlGet, controlhwnd, Hwnd, %A_GuiControl% ; hwnd of control pressed

	; Search
	if (controlhwnd = GoHwnd || !A_GuiControl)
	{
		if SEARCH
		{
			Gui, Submit
			
			if instr(A_ThisHotkey, "^") ; if control is held for a website
			{
				Run % instr(GSEARCH, ".com") ? "www." GSEARCH : "www." GSEARCH ".com"
				Gui, Destroy
				return
			}
			
			Google(GSEARCH) ; regular google search
			Gui, Destroy
			return
		}
	}
	
Gui, Submit, NoHide
	
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;USER COMMANDS FROM SETTINGS INI

	COMMAND := USER_INI.MainButtons[A_GuiControl]
		
	Execute(COMMAND)
return



3ButtonPress:
NEXT_KILL := A_GUI + 1

SetTitleMatchMode, 3
if !(WinExist(A_GuiControl))
	Gui, %NEXT_KILL%:Destroy ; kill gui 2 before in case another gui is selected
SetTitleMatchMode, 1
WinGetActiveTitle, A_TITLE ; get title of gui 2

if (A_TITLE = "Bookmarks")
{
	MyFavs := A_MyDocuments "\..\Favorites\Links"
	Run, % "explorer.exe " MyFavs "\" A_GuiControl ".url"
	Gui, 1:Destroy
	return
}

COMMAND := USER_INI[A_TITLE][A_GuiControl] ; set command var from user settings

Execute(COMMAND) ; execute command
return




closer:
ExitApp
return








